<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog" :class="{'active':active}">
      <span class="placeholder" v-if="!deafultaddress.eveny">请选择配送地址</span>
      <span class="value" v-else >{{deafultaddress.eveny}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
        <div class="loading" v-if="!arr.length"></div>
        <template v-else>
      <span class="ellipsis" v-for="i in list" :key="i.code" @click="address(i)">{{i.name}}</span>

        </template>
    </div>
  </div>
</template>
<script>
import { computed, reactive, ref } from 'vue'
import axios from 'axios'
// 这个是组合api用于点击目标以外的地方和点击目标发生变化
import { onClickOutside } from '@vueuse/core'
export default {
  name: 'XtxCity',
  setup () {
    //    active:是否显示地址弹窗
    const active = ref(false)
    // 接收数据
    const arr = ref([])
    const getcity = async () => {
      // 发请求获取地址数据
      const { data } = await axios('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
      return data
    }
    // 打开地址弹窗
    const open = async () => {
      active.value = true
      if (!window.citydata) {
        window.citydata = await getcity()
        arr.value = window.citydata
      } else {
        arr.value = window.citydata
      }
      //  第二次填写地址之前 清空数据
      for (const key in deafultaddress) {
        deafultaddress[key] = ''
      }
    }
    // 关闭弹窗
    const hide = () => {
      active.value = false
    }
    // 点击弹窗按钮事件
    const toggleDialog = () => {
      if (active.value) {
        hide()
      } else {
        open()
      }
    }

    const target = ref(null)
    // 组合api点击其他地方关闭弹窗
    onClickOutside(target, (event) => {
      hide()
    })
    // 定义默认地址对象
    const deafultaddress = reactive({
      provinceCode: '', // 省份编码
      provinceName: '',
      cityCode: '', // 城市编码
      cityName: '',
      countyCode: '', // 区编码
      countyName: '',
      eveny: '' // 所有数据合集
    })

    // 选择地址函数
    const address = (i) => {
      if (i.level === 0) {
        deafultaddress.provinceCode = i.code
        deafultaddress.provinceName = i.name
      } else if (i.level === 1) {
        deafultaddress.cityCode = i.code
        deafultaddress.cityName = i.name
      } else {
        deafultaddress.countyCode = i.code
        deafultaddress.countyName = i.name
        deafultaddress.eveny = `${deafultaddress.provinceName}-${deafultaddress.cityName}-${deafultaddress.countyName}`
        hide()
      }
    }
    const list = computed(() => {
      let arrs = arr.value
      if (deafultaddress.provinceCode) {
        arrs = arrs.find(v => v.code === deafultaddress.provinceCode).areaList
      }
      if (deafultaddress.cityCode) {
        arrs = arrs.find(v => v.code === deafultaddress.cityCode).areaList
      }
      if (deafultaddress.countyCode) {
        arrs = arrs.find(v => v.code === deafultaddress.countyCode)
      }
      return arrs
    })

    return { toggleDialog, active, target, arr, address, list, deafultaddress }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
  .option {
        .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
}
}
</style>
